﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Views.Base;
using Model;

namespace TanHungLong
{
    public partial class FrmTaiKhoanKeToan : DevExpress.XtraEditors.XtraForm
    {
        #region variant
        private FrmMain main = null;
        private FormStatus formStatus = FormStatus.View;
        private bool allowCloseForm = true;
        private TaiKhoanKTBOCollection taiKhoanKTBOCollection = null;

        private string COLUMN_FIRST = "StrSoHieu1";
        private string COLUMN_SECOND = "StrSoHieu2";
        private Font fCap1 = new Font("Tahoma", 8.25f, FontStyle.Bold);
        private Font fCap2 = new Font("Tahoma", 8.25f, FontStyle.Regular);
        private Font fCap3 = new Font("Tahoma", 8.25f, FontStyle.Italic);

        private Font fCap1_del = new Font("Tahoma", 8.25f, (FontStyle.Bold | FontStyle.Strikeout));
        private Font fCap2_del = new Font("Tahoma", 8.25f, (FontStyle.Regular | FontStyle.Strikeout));
        private Font fCap3_del = new Font("Tahoma", 8.25f, (FontStyle.Italic | FontStyle.Strikeout));

        private CapTaiKhoanKTBOList capTaiKhoanKTBOList = null;
        #endregion variant

        //=====================================
        #region form
        public FrmTaiKhoanKeToan()
        {
            InitializeComponent();
            this.KeyPreview = true;

            this.Load += new EventHandler(FrmTaiKhoanKeToan_Load);
            this.Shown += new EventHandler(FrmTaiKhoanKeToan_Shown);
            this.KeyDown += new KeyEventHandler(FrmTaiKhoanKeToan_KeyDown);
        }

        private void FrmTaiKhoanKeToan_KeyDown(object sender, KeyEventArgs e)
        {
            if (this.btnClose.Visible && ((e.KeyCode == Keys.Q && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F10)))
                this.Close();
            else
                if (this.btnRefresh.Visible && ((e.KeyCode == Keys.R && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F5)))
                    RefreshData();
                else
                    if (this.btnSave.Visible && ((e.KeyCode == Keys.S && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F6)))
                        Save();
                    else
                        if (this.btnEdit.Visible && ((e.KeyCode == Keys.E && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F2)))
                            Edit();
                        else
                            if (this.btnCancel.Visible && ((e.KeyCode == Keys.H && Control.ModifierKeys == Keys.Control) || (e.KeyCode == Keys.F12)))
                                Cancel();
        }

        private void FrmTaiKhoanKeToan_Shown(object sender, EventArgs e)
        {
            this.bandView.Focus();
        }

        private void FrmTaiKhoanKeToan_Load(object sender, EventArgs e)
        {
            this.btnRefresh.Click += new EventHandler(btnRefresh_Click);
            this.btnEdit.Click += new EventHandler(btnEdit_Click);
            this.btnSave.Click += new EventHandler(btnSave_Click);
            this.btnCancel.Click += new EventHandler(btnCancel_Click);
            this.btnClose.Click += new EventHandler(btnClose_Click);

            this.bandView.RowCellStyle += new RowCellStyleEventHandler(bandView_RowCellStyle);
            this.bandView.CustomDrawGroupRow += new RowObjectCustomDrawEventHandler(bandView_CustomDrawGroupRow);

            this.FormClosing += new FormClosingEventHandler(FrmTaiKhoanKeToan_FormClosing);

            //------------
            Utility.InitControl(this.repLookUpCapTaiKhoan);

            RefreshData();
            SetFormStatus(this.formStatus);
        }

        private void FrmTaiKhoanKeToan_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (this.allowCloseForm == false)
                e.Cancel = true;
            else
                if (this.main != null)
                {
                    this.main.Controls.Remove(this);
                    this.main.HideBgr();
                    this.Dispose();
                }
        }
        #endregion form

        //=====================================
        #region control
        #endregion control

        //=====================================
        #region button
        private void btnRefresh_Click(object sender, EventArgs e)
        {
            RefreshData();
        }

        private void btnEdit_Click(object sender, EventArgs e)
        {
            Edit();
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            Save();
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Cancel();
        }

        private void btnClose_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion button

        //=====================================
        #region grid
        private void bandView_CustomDrawGroupRow(object sender, RowObjectCustomDrawEventArgs e)
        {
            GridGroupRowInfo info = e.Info as GridGroupRowInfo;

            if (info.Column.FieldName == "CapTaiKhoan")
            {
                int CapTaiKhoan = StaticUtil.GetInt(info.EditValue);
                if (this.capTaiKhoanKTBOList.ContainsKey(CapTaiKhoan))
                    info.GroupText = this.capTaiKhoanKTBOList.Value(CapTaiKhoan);
            }
        }

        private void bandView_RowCellStyle(object sender, RowCellStyleEventArgs e)
        {
            bool suDung = StaticUtil.GetBool(this.bandView.GetRowCellValue(e.RowHandle, "SuDung"));
            int cap = 2;
            if (string.IsNullOrEmpty(this.bandView.GetRowCellDisplayText(e.RowHandle, "StrSoHieu1")) == false)
                cap = 1;
            else
                if (string.IsNullOrEmpty(this.bandView.GetRowCellDisplayText(e.RowHandle, "StrSoHieu2")) == false)
                    cap = 2;
                else
                    if (string.IsNullOrEmpty(this.bandView.GetRowCellDisplayText(e.RowHandle, "StrSoHieu3")) == false)
                        cap = 3;

            Font font = null;
            if (!suDung)
                font = (cap == 1 ? fCap1_del : cap == 2 ? fCap2_del : fCap3_del);
            else
                font = (cap == 1 ? fCap1 : cap == 2 ? fCap2 : fCap3);

            e.Appearance.Font = font;
            e.Appearance.ForeColor = (!suDung ? MyStyle.DELETE_COLOR : Color.Black);
        }
        #endregion grid

        //=====================================
        #region function
        private void RefreshData()
        {
            this.capTaiKhoanKTBOList = CapTaiKhoanKTBOList.NewCapTaiKhoanKTBOList();
            this.capTaiKhoanKTBOList.AddCustomValue(int.MaxValue, "Nhập mới");
            Utility.SetData(this.repLookUpCapTaiKhoan, capTaiKhoanKTBOList, capTaiKhoanKTBOList.Count, capTaiKhoanKTBOList.Count, DevExpress.XtraEditors.Controls.TextEditStyles.DisableTextEditor);

            this.taiKhoanKTBOCollection = TaiKhoanKTBOCollection.GetTaiKhoanKTBOCollection();
            this.bdSource.DataSource = this.taiKhoanKTBOCollection.Clone();
        }

        private void Edit()
        {
            this.formStatus = FormStatus.Edit;
            SetFormStatus(this.formStatus);
            Session.CloseIndex++;
        }

        private void Save()
        {
            string oldColumn = this.bandView.FocusedColumn.FieldName;
            string newColumn = (oldColumn == this.COLUMN_FIRST ? this.COLUMN_SECOND : this.COLUMN_FIRST);
            this.bandView.FocusedColumn = this.bandView.Columns[newColumn];

            this.bdSource.EndEdit();
            this.taiKhoanKTBOCollection = (TaiKhoanKTBOCollection)this.bdSource.DataSource;


            int length = this.taiKhoanKTBOCollection.Count;
            TaiKhoanKTBO taiKhoanBO = null;
            for (int i = 0; i < length; i++)
            {
                taiKhoanBO = this.taiKhoanKTBOCollection[i];
                if (taiKhoanBO.SoHieu1.HasValue)
                {
                    taiKhoanBO.CapTaiKhoan = (taiKhoanBO.SoHieu1.Value / 100);
                    if (taiKhoanBO.CapTaiKhoan == 0)
                        taiKhoanBO.CapTaiKhoan = 10;
                }
                else
                    taiKhoanBO.CapTaiKhoan = 10;
            }

            this.taiKhoanKTBOCollection = this.taiKhoanKTBOCollection.Save();
            this.taiKhoanKTBOCollection.Sort();
            this.bdSource.DataSource = this.taiKhoanKTBOCollection.Clone();


            this.bandView.FocusedColumn = this.bandView.Columns[oldColumn];
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;
        }

        private void Cancel()
        {
            this.formStatus = FormStatus.View;
            SetFormStatus(this.formStatus);
            Session.CloseIndex--;
            this.bdSource.DataSource = this.taiKhoanKTBOCollection.Clone();
        }

        private void SetFormStatus(FormStatus formStatus)
        {
            bool edit = (formStatus != FormStatus.View);
            this.allowCloseForm = !edit;

            MyStyle.SetGridStyle(this.bandView, edit);

            this.btnEdit.Visible = !edit;
            this.btnClose.Visible = !edit;
            this.btnRefresh.Visible = !edit;

            this.btnSave.Visible = edit;
            this.btnCancel.Visible = edit;

            this.band1.Visible = edit;
            foreach (GridColumn col in this.bandView.Columns)
            {
                if (col.Visible)
                    col.OptionsColumn.AllowEdit = edit;
            }

            this.bandView.OptionsView.NewItemRowPosition = (edit ? NewItemRowPosition.Bottom : NewItemRowPosition.None);
            this.bandView.Focus();

            if (edit)
            {
                string focusColumn = this.bandView.FocusedColumn.FieldName;

                if (string.IsNullOrEmpty(focusColumn))
                    focusColumn = this.COLUMN_FIRST;

                if (this.bandView.RowCount == 1)
                    this.bandView.FocusedRowHandle = int.MinValue + 1;

                this.bandView.FocusedColumn = this.bandView.Columns[focusColumn];
                this.bandView.ShowEditor();
            }
        }
        #endregion function
    }
}